﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using TravelSite.DAL;
using TravelSite.DAL.Repositories;
using TravelSite.Entities;
using TravelSite.Code;

namespace Travel.Api.Site
{
    public partial class TravelPackagePage : BaseDataPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }

        public override void LoadData()
        {
            using (var repo = RepositoryFactory.ResolveRepository<ApiTravelPackageRepository>())
            {
                grid.DataSource = repo.FindByClient(ContactId);
                grid.DataBind();
            }
        }

        protected void grid_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grid.PageIndex = e.NewPageIndex;
            LoadData();
        }

        protected void grid_RowCommand(object sender, CommandEventArgs e)
        {
            int travelId;
            int pkgDestId;

            string[] args = e.CommandArgument.ToString().Split(';');
            travelId = Convert.ToInt32(args[0]);

            using (var repo = RepositoryFactory.ResolveRepository<ApiTravelPackageRepository>())
            {
                switch (e.CommandName)
                {
                    case "Book":
                        DropDownList ddl = (DropDownList)((sender as Button).NamingContainer as GridViewRow).FindControl("ddlDestination");
                        pkgDestId = Convert.ToInt32(ddl.SelectedValue);
                        Response.Redirect(string.Format("~/Client/OptionChoice.aspx?tid={0}&pdid={1}", travelId.ToString(), pkgDestId.ToString()));
                        break;
                    case "View":
                        pkgDestId = Convert.ToInt32(args[1]);
                        Response.Redirect(string.Format("~/Client/OptionChoice.aspx?tid={0}&pdid={1}", travelId.ToString(), pkgDestId.ToString()));
                        break;
                }
            }
            LoadData();
        }

        protected void grid_RowDataBound(Object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                TravelPackage item = (TravelPackage)e.Row.DataItem;
                
                DropDownList ddl = (DropDownList)e.Row.FindControl("ddlDestination");
                Label lbl = (Label)e.Row.FindControl("lblNameDestination");
                Button btn = (Button)e.Row.FindControl("btnDestination");

                ddl.Visible = false;
                lbl.Visible = false;
                if (item.PkgDestId == -1)
                {
                    ddl.Visible = true;
                    ddl.DataSource = item.Destinations;
                    ddl.DataBind();

                    btn.Text = btn.CommandName = "Book";
                    btn.CommandArgument = item.TravelId.ToString() + ";-1";
                }
                else
                {
                    lbl.Visible = true;
                    lbl.Text = item.Destinations.Where(i => i.PkgDestId == item.PkgDestId).SingleOrDefault().DestinationName;
                    if (item.Status != "CANCELED")
                    {
                        btn.Text = btn.CommandName = "View";
                        btn.CommandArgument = item.TravelId.ToString() + ";" + item.PkgDestId.ToString();
                    }
                    else
                        btn.Visible = false;
                }

                lbl = (Label)e.Row.FindControl("lblExpDate");
                if (item.ExpDate.HasValue)
                    lbl.Text = item.ExpDate.Value.ToString("d");
            }
        }        
    }
}